package kr.saeildamil.androidforstudy2.ch31;

import java.util.ArrayList;

import kr.saeildamil.androidforstudy2.R;
import android.app.Activity;
import android.gesture.Gesture;
import android.gesture.GestureLibraries;
import android.gesture.GestureLibrary;
import android.gesture.GestureOverlayView;
import android.gesture.Prediction;
import android.gesture.GestureOverlayView.OnGesturePerformedListener;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class GestureOverlay extends Activity {
	WebView mWeb;
	GestureLibrary mLibrary;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ch31_gestureoverlay);

		mWeb = (WebView) findViewById(R.id.ch31_gestureoverlay_webview);
		mWeb.setWebViewClient(new MyWebClient());
		WebSettings set = mWeb.getSettings();
		set.setJavaScriptEnabled(true);
		set.setBuiltInZoomControls(true);
		mWeb.loadUrl("http://m.naver.com");

		// mLibrary = GestureLibraries.fromRawResource(this, R.raw.gestures);

		GestureOverlayView gestures = (GestureOverlayView) findViewById(R.id.gestures);
		gestures.addOnGesturePerformedListener(mListener);
	}

	class MyWebClient extends WebViewClient {
		@Override
		public boolean shouldOverrideUrlLoading(WebView view, String url) {
			return super.shouldOverrideUrlLoading(view, url);
		}
	}

	OnGesturePerformedListener mListener = new OnGesturePerformedListener() {

		@Override
		public void onGesturePerformed(GestureOverlayView overlay,
				Gesture gesture) {
			ArrayList<Prediction> predictions = mLibrary.recognize(gesture);

			if (predictions.size() != 0) {
				Prediction prediction = predictions.get(0);
				String name = prediction.name;
				if (prediction.score > 1.0) {
					if (name.equalsIgnoreCase("Kiyuk")) {
						mWeb.goBack();
					} else if (name.equalsIgnoreCase("Niun")) {
						mWeb.goForward();
					} else if (name.equalsIgnoreCase("Digut")) {
						mWeb.loadUrl("http://www.google.com");
					} else {
						mWeb.loadUrl("http://m.daum.net");
					}
				}
			}
		}

	};

}
